package com.graduation_design.modules.app.controller;

import cn.hutool.core.map.MapUtil;
import com.graduation_design.common.dto.AppOrderDto;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppOrder;
import com.graduation_design.modules.app.annotation.Login;
import com.graduation_design.modules.app.annotation.NoRepeatSubmit;
import com.graduation_design.service.AppCartItemService;
import com.graduation_design.service.AppOrderService;
import com.graduation_design.service.AppUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/app/order")
public class OrderController extends BaseController {

    @Resource
    AppCartItemService appCartItemService;

    @Resource
    AppUserService appUserService;

    @Resource
    AppOrderService appOrderService;

    /**
     * 下单前购买预览
     */
    @Login
    @PostMapping("/preview")
    public Result preview(@RequestBody AppOrderDto orderDto) {
        return Result.success(appOrderService.preview(orderDto));
    }

    /**
     * 创建订单
     * @NoRepeatSubmit 拦截该注解的方法,避免重复下单
     */
    @Login
    @NoRepeatSubmit
    @PostMapping("/create")
    public Result create(@RequestBody AppOrderDto orderDto){
        return Result.success(appOrderService.create(orderDto));
    }

    /**
     * 订单计数
     */
    @Login
    @GetMapping("/count")
    public Result getOrderCount() {
        return Result.success(appOrderService.getOrderCount());
    }

    /**
     * 订单列表
     */
    @Login
    @GetMapping("/list")
    public Result list(Integer status) {
        long userId = appUserService.getCurrentUserId();
        return Result.success(appOrderService.getPage(getPage(), userId, status));
    }

    /**
     * 订单详情
     */
    @Login
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable long id) {
        return Result.success(appOrderService.detail(id));
    }

    /**
     * 订单取消
     */
    @Login
    @PostMapping("/cancel")
    public Result cancel(Long id) {
        return Result.success(appOrderService.cancel(id));
    }

    /**
     * 删除订单
     */
    @Login
    @PostMapping("/delete")
    public Result delete(Long id) {
        return Result.success(appOrderService.delete(id));
    }

    /**
     * 订单物流
     */
    @Login
    @GetMapping("/deliveryInfo")
    public Result deliveryInfo(Long id) throws Exception {
        AppOrder appOrder = appOrderService.getOwnById(id);
        return Result.success(
                MapUtil.builder()
                        .put("deliveryInfo", appOrderService.getDeliveryInfo(appOrder))
                        .put("deliveryCompany", appOrder.getDeliveryCompany())
                        .put("deliverySn", appOrder.getDeliverySn())
                        .build()
        );
    }

    /**
     * 确认收货
     */
    @Login
    @PostMapping("/confirm")
    public Result confirm(Long id) {
        appOrderService.confirm(id);
        return Result.success();
    }

}
